package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.caocaog.market.entity.ShopType;
import com.caocaog.market.entity.dto.ShopTypeTree;
import com.caocaog.market.service.ShopTypeService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.base.entity.groups.InsertGroup;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 店铺分类
 *
 * @author chechen
 * @date 2022-12-11
 */
@Slf4j
@RestController
@RequestMapping("/shopType")
public class ShopTypeController extends GenericController<ShopType, ShopTypeService> {

    public static final String TABLE_MEANING = "店铺加价配置 - ";


    @GetMapping(value = "/getTree")
    public Result<List<ShopTypeTree>> getShopTypeTree() {
        String siteId = GenericController.dealWithBranchId();
        return Result.OK(super.service.getShopTypeTree(siteId, siteId == null));
    }


    @GetMapping(value = "/getById")
    public Result<ShopType> getShopTypeById(@RequestParam Long id) {
        return Result.OK(super.service.getShopTypeById(id));
    }


    @GetMapping(value = "/getAllList")
    public Result<List<ShopType>> getAllShopTypeList() {
        return Result.OK(super.service.getAllShopTypeList());
    }


    @AutoLog(value = TABLE_MEANING + ADD)
    @PostMapping(value = "/add")
    public Result<Integer> add(@RequestBody @Validated({InsertGroup.class}) ShopType shopType) {
        shopType.clearFields();
        int addRecordCount = super.service.addShopType(shopType);
        if (addRecordCount > 0)
            return Result.OK(ADD_SUCCESS, addRecordCount);
        else return Result.error("新增失败", 0);
    }


    @AutoLog(value = TABLE_MEANING + EDIT)
    @PutMapping(value = "/edit")
    public Result<Integer> edit(@RequestBody @Validated({UpdateGroup.class}) ShopType shopType) {
        shopType.clearFields();
        int updateRecordCount = super.service.updateShopType(shopType);
        if (updateRecordCount > 0)
            return Result.OK(EDIT_SUCCESS, updateRecordCount);
        else return Result.error("编辑失败", 0);
    }


    @AutoLog(value = TABLE_MEANING + DELETE_BATCH)
    @DeleteMapping(value = "/delete")
    public Result<Integer> delete(@RequestParam String ids) {
        Long[] idArray = Arrays.stream(ids.split(StringPool.COMMA)).map(Long::valueOf).toArray(Long[]::new);
        int deleteRecordCount = super.service.deleteShopType(idArray);
        if (deleteRecordCount > 0)
            return Result.OK(DELETE_SUCCESS, deleteRecordCount);
        else return Result.error("删除失败", 0);
    }


    @AutoLog(value = TABLE_MEANING + "变更状态")
    @PutMapping(value = "/changeStatus")
    public Result<Integer> changeShopTypeStatus(@RequestParam Long id) {
        int updateRecordCount = super.service.changeShopTypeStatus(id);
        if (updateRecordCount > 0)
            return Result.OK(EDIT_SUCCESS, updateRecordCount);
        else return Result.error("变更状态失败", 0);
    }


    @AutoLog(value = TABLE_MEANING + "添加分站ids到黑名单中")
    @PutMapping(value = "/addBlacklist")
    public Result<Integer> addBlacklistInShopType(@RequestParam Long id, @RequestParam String siteIds) {
        String[] siteIdArray = Arrays.stream(siteIds.split(StringPool.COMMA)).toArray(String[]::new);
        int updateRecordCount = super.service.addBlacklistInShopType(id, siteIdArray);
        if (updateRecordCount > 0)
            return Result.OK(EDIT_SUCCESS, updateRecordCount);
        else return Result.error("添加分站ids到黑名单中失败", 0);
    }


}
